const eduUnitTypes = [
  'academy',
  'department',
  'speciality'
]

module.exports = mongoose => {
  let model = {Schemas: {}}

  academy:
  {
    let schema = model.Schemas.Academy = new mongoose.Schema({
      name: {type: String, unique: true},
      type: {type: String, default: 'academy', enum: ['academy']},

      department_ids: {type: [mongoose.Schema.Types.ObjectId], default: undefined},
      departments: {type: [model.Schemas.Department], default: undefined}
    }, {collection: 'education_unit'})

    schema.pre('save', function (next) {
      this.type = 'academy'
      next()
    })
    model.academy = mongoose.model('Academy', schema)
  }

  department:
  {
    let schema = model.Schemas.Department = new mongoose.Schema({
      name: {type: String, unique: true},
      type: {type: String, default: 'department', enum: ['department']},

      academy_id: {type: mongoose.Schema.Types.ObjectId, required: false},

      speciality_ids: {type: [mongoose.Schema.Types.ObjectId], default: undefined}
    }, {collection: 'education_unit'})

    schema.pre('save', function (next) {
      this.type = 'department'
      next()
    })
    model.department = mongoose.model('Department', schema)
  }

  speciality:
  {
    let schema = model.Schemas.Speciality = new mongoose.Schema({
      name: {type: String, unique: true},
      type: {type: String, default: 'speciality', enum: ['speciality']},

      department_id: {type: mongoose.Schema.Types.ObjectId, required: false},
      academy_id: {type: mongoose.Schema.Types.ObjectId, required: false},
    }, {collection: 'education_unit'})

    schema.pre('save', function (next) {
      this.type = 'speciality'
      next()
    })
    model.speciality = mongoose.model('Speciality', schema)
  }

  return model
}
